<?php

class AdodbDriver extends BaseDriver {
    
    /**
     * @var      ADOConnection
     */
    protected $adodb;
    
    /**
     * @param    array $config
     * @return   void
     */
    public function __construct($config) {
        include_once(ROOT.'/libs/vendor/adodb5/adodb.inc.php');
        if (isset($config['dsn'])) {
            $this->adodb = NewADOConnection($config['dsn']);
//            $this->adodb->debug = true;
            $this->config = $config;
            $this->prefix = $config['prefix'];
        } else {
            $this->adodb = NewADOConnection($config['db']);
            $this->adodb->Connect($config['host'], $config['login'], $config['password'], $config['database']);
            $this->config = $config;
            $this->prefix = $config['prefix'];
        }
//        $this->adodb->SetFetchMode(ADODB_FETCH_ASSOC);
        if (method_exists($this->adodb, 'SetCharSet') && isset($config['encoding'])) {
            $this->adodb->SetCharSet($config['encoding']);
        }
    }
    
    /**
     * @param    string $sql
     * @return   PdoRecordSet
     */
    public function query($sql, $param = array()) {
        $sql = str_replace('{#prefix#}', $this->config['prefix'], $sql);
        $rs = $this->adodb->Execute($sql, $param);
        return new AdodbRecordSet($rs, $sql);
    }
    
    /**
     * @param    string $sql
     * @return   int
     */
    public function exec($sql) {
        $ret = $this->adodb->Execute($sql);
        return $ret;
    }
    
    /**
     * @param    string $table
     * @param    array $record
     * @return   int
     */
    public function update($table, $record, $condition) {
        $table = $this->prefix.$table;
        return $this->adodb->AutoExecute($table, $record, 'UPDATE', $condition);
    }
    
    /**
     * @param    string $table
     * @param    array $record
     * @return   int
     */
    public function insert($table, $record) {
        $table = $this->prefix.$table;
        return $this->adodb->AutoExecute($table, $record, 'INSERT', false);
    }
    
    /**
     * @param    string $table
     * @param    array $condition    
     * @return   PdoRecordSet
     */
    public function select($table, $condition = array(), $fields = '*', $order = null, $limit = null) {
        if (!empty($conditions)) {
            $where = " WHERE";
            foreach ($conditions as $field => $value) {
                $where .= " $field = ?";
                $values[] = $value;
            }
        } else {
            $where = '';
            $values = array();
        }
        $order = empty($order) ? '' : " ORDER BY $order";
        $limit = empty($limit) ? '' : " LIMIT $limit";
        $sql = "SELECT $fields FROM `{$this->prefix}$table`$where$order$limit";
        $rs = $this->adodb->Execute($sql, $values);
        return new AdodbRecordSet($rs, $sql);
    }
    
    public function delete($table, $condition) {
        $sql = "DELETE FROM ".$this->prefix."$table WHERE $condition";
        return $this->exec($sql);
    }
    
    public function getLastInsertId() {
        return $this->adodb->Insert_ID();
    }
    
    /**
     * 取得数据库连接
     *
     * @return PDO
     */
    public function getConnection() {
        return $this->adodb;
    }
}

